using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Web.Base.ActionResults
{
    public class JsonpResult<T> : ActionResult
    {
        public T Data { get; set; }
        public string CallbackName { get; set; }

        public JsonpResult(T data, string callback)
        {
            Data = data;
            CallbackName = callback;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var js = new JavaScriptSerializer();
            var jsonp = CallbackName + "(" + js.Serialize(Data) + ")";
            context.HttpContext.Response.ContentType = "application/javascript";
            context.HttpContext.Response.Clear();
            context.HttpContext.Response.Write(jsonp);
            context.HttpContext.Response.End();
        }
    }
}